package main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.nio.client.DefaultHttpAsyncClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.nio.client.HttpAsyncClient;




public class Connection {
	public static String accesstoken;
	private static final String BASE_URL = "http://192.168.1.8/";
	private static final String BASE_URL2 = "http://192.168.1.8/";
    public HttpAsyncClient httpclient;
    public static int numberConnection=0;
    public static List<HttpAsyncClient> listClient = new ArrayList<HttpAsyncClient>();
    public Connection() throws Exception{
    	
    	
    	
    	if(numberConnection%10==0){
    		
    		listClient.add( new DefaultHttpAsyncClient() );
    		listClient.get(numberConnection/10).start();
    	}
    	
    	httpclient = listClient.get(numberConnection/10);
    	numberConnection++;
    }
    public void post(String uri,List<NameValuePair> nameValuePairs, FutureCallback<HttpResponse> callback) throws UnsupportedEncodingException{
    	HttpPost post = new HttpPost(BASE_URL+uri);
    	
    	post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    	
    	Future<HttpResponse> future =httpclient.execute(post,callback);
    }
	 public static void main(String[] args) throws Exception {
		 	
	        /*
	        //conn.httpclient.start();
	        try {
	            HttpPost request = new HttpPost("http://192.168.1.7");
	            //request.addHeader("abc", "Def");
	            
	            System.out.println(request.toString());
	           FutureCallback<HttpResponse> callback= new FutureCallback<HttpResponse>() {

                    public void completed(final HttpResponse response) {
                    	 String myString;
						try {
							myString = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
							System.out.println(myString);
						} catch (IllegalStateException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
         	            
                        System.out.println( "->" + response.getStatusLine());
                    }

                    public void failed(final Exception ex) {
                        
                        System.out.println( "->" + ex);
                    }

                    public void cancelled() {
                        
                        System.out.println( " cancelled");
                    }

                };
	            Future<HttpResponse> future = conn.httpclient.execute(request, callback);
	            HttpResponse response = future.get();
	            //BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
	            //String line = "";
	           
	           // while ((line = rd.readLine()) != null) {
	             // System.out.println(line);
	            //}

	        } finally {
	            conn.httpclient.shutdown();
	        }
	        System.out.println("Done");
	    }
	    */
	 }

}
